/*
 * @Author: wuhao930406 1148547900@qq.com
 * @Date: 2023-09-06 14:17:58
 * @LastEditors: wuhao930406 1148547900@qq.com
 * @LastEditTime: 2023-10-30 11:00:09
 * @FilePath: /utools_web/src/utils/time.js
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */

export function difftime(start, end) {
  const diffInMs = start.diff(end);
  if (diffInMs < 60 * 60 * 1000) {
    // 时间差小于 1 小时，转为分钟
    const diffInMinutes = Math.round(diffInMs / (60 * 1000));
    return diffInMinutes + '分钟前';
  } else {
    // 时间差大于等于 1 小时，继续判断是否超过 24 小时
    const diffInHours = Math.round(diffInMs / (60 * 60 * 1000));
    if (diffInHours >= 24) {
      // 时间差超过 24 小时，转为天
      const diffInDays = Math.round(diffInHours / 24);
      return diffInDays + '天前';
    } else {
      // 时间差在 1 小时到 24 小时之间，直接输出小时数
      return diffInHours + '小时前';
    }
  }
}

export function difftimes(start, end, status, format, time) {
  const diffInMs = start.diff(end);

  const text = status === 0 || status === 1 ? '超' : '完成';
  if (diffInMs < 0) {
    const diffInMx = end.diff(start);
    if (diffInMx < 60 * 60 * 1000) {
      // 时间差小于 1 小时，转为分钟
      const diffInMinutes = Math.round(diffInMx / (60 * 1000));
      switch (format) {
        case 'format1':
          return (
            <span style={{ color: '#999', fontSize: 12, display: "inline-flex", alignItems: "baseline", gap: 2 }}>
              {text}
              <b style={{ color: text === '超' ? '#ff4800' : 'inherit' }}>
                {diffInMinutes}
              </b>
              分钟
            </span>
          );
        default:
          return text + diffInMinutes + '分钟';
      }
    } else {
      // 时间差大于等于 1 小时，继续判断是否超过 24 小时
      const diffInHours = Math.round(diffInMx / (60 * 60 * 1000));
      if (diffInHours >= 24) {
        // 时间差超过 24 小时，转为天
        const diffInDays = Math.round(diffInHours / 24);

        switch (format) {
          case 'format1':
            return (
              <span style={{ color: '#999', fontSize: 12, display: "inline-flex", alignItems: "baseline", gap: 2 }}>
                {text}
                <b style={{ color: text === '超' ? '#ff4800' : 'inherit' }}>
                  {diffInDays}
                </b>
                天
              </span>
            );
          default:
            return text + diffInDays + '天';
        }
      } else {
        // 时间差在 1 小时到 24 小时之间，直接输出小时数
        switch (format) {
          case 'format1':
            return (
              <span style={{ color: '#999', fontSize: 12, display: "inline-flex", alignItems: "baseline", gap: 2 }}>
                {text}
                <b style={{ color: text === '超' ? '#ff4800' : 'inherit' }}>
                  {diffInHours}
                </b>
                小时
              </span>
            );
          default:
            return text + diffInHours + '小时';
        }
      }
    }
  } else {
    const dom = null;
    if (diffInMs < 60 * 60 * 1000) {
      // 时间差小于 1 小时，转为分钟
      const diffInMinutes = Math.round(diffInMs / (60 * 1000));
      switch (format) {
        case 'format1':
          return (
            <span
              style={{ color: '#999', fontSize: 12, display: 'flex', alignItems: 'center', gap: 2 }}
            >
              {dom}
              <div style={{ paddingTop: dom ? 2 : 0 }}>
                剩余
                <b>{diffInMinutes}</b>
                分钟
              </div>
            </span>
          );
        default:
          return '剩余' + diffInMinutes + '分钟';
      }
    } else {
      // 时间差大于等于 1 小时，继续判断是否超过 24 小时
      const diffInHours = Math.round(diffInMs / (60 * 60 * 1000));
      if (diffInHours >= 24) {
        // 时间差超过 24 小时，转为天
        const diffInDays = Math.round(diffInHours / 24);
        switch (format) {
          case 'format1':
            return (
              <span
                style={{
                  color: '#999',
                  fontSize: 12,
                  display: 'flex',
                  alignItems: 'center',
                  gap: 2,
                }}
              >
                {dom}
                <div style={{ paddingTop: dom ? 2 : 0 }}>
                  剩余
                  <b> {diffInDays} </b>天
                </div>
              </span>
            );
          default:
            return '剩余' + diffInDays + '天';
        }
      } else {
        // 时间差在 1 小时到 24 小时之间，直接输出小时数

        switch (format) {
          case 'format1':
            return (
              <span
                style={{
                  color: '#999',
                  fontSize: 12,
                  display: 'flex',
                  alignItems: 'center',
                  gap: 2,
                }}
              >
                {dom}
                <div style={{ paddingTop: dom ? 2 : 0 }}>
                  剩余
                  <b> {diffInHours} </b>
                  小时
                </div>
              </span>
            );
          default:
            return '剩余' + diffInHours + '小时';
        }
      }
    }
  }
}

export function difftimer(start, end, format, time) {
  const diffInMs = start.diff(end);

  const text = '后延';
  if (diffInMs < 0) {
    const diffInMx = end.diff(start);
    if (diffInMx < 60 * 60 * 1000) {
      // 时间差小于 1 小时，转为分钟
      const diffInMinutes = Math.round(diffInMx / (60 * 1000));
      switch (format) {
        case 'format1':
          return (
            <span style={{ color: '#999', fontSize: 12 }}>
              {text}
              <b style={{ color: text === '后延' ? '#ff4800' : 'inherit', fontSize: 14 }}>

                {diffInMinutes}
              </b>
              分钟
            </span>
          );
        default:
          return text + diffInMinutes + '分钟';
      }
    } else {
      // 时间差大于等于 1 小时，继续判断是否超过 24 小时
      const diffInHours = Math.round(diffInMx / (60 * 60 * 1000));
      if (diffInHours >= 24) {
        // 时间差超过 24 小时，转为天
        const diffInDays = Math.round(diffInHours / 24);

        switch (format) {
          case 'format1':
            return (
              <span style={{ color: '#999', fontSize: 12 }}>
                {text}
                <b style={{ color: text === '后延' ? '#ff4800' : 'inherit', fontSize: 14 }}>

                  {diffInDays}
                </b>
                天
              </span>
            );
          default:
            return text + diffInDays + '天';
        }
      } else {
        // 时间差在 1 小时到 24 小时之间，直接输出小时数
        switch (format) {
          case 'format1':
            return (
              <span style={{ color: '#999', fontSize: 12 }}>
                {text}
                <b style={{ color: text === '后延' ? '#ff4800' : 'inherit', fontSize: 14 }}>

                  {diffInHours}
                </b>
                小时
              </span>
            );
          default:
            return text + diffInHours + '小时';
        }
      }
    }
  } else {
    if (diffInMs < 60 * 60 * 1000) {
      // 时间差小于 1 小时，转为分钟
      const diffInMinutes = Math.round(diffInMs / (60 * 1000));
      switch (format) {
        case 'format1':
          return (
            <span
              style={{ color: '#999', fontSize: 12, display: 'flex', alignItems: 'center', gap: 2 }}
            >
              <div style={{ paddingTop: 0 }}>
                提前
                <b style={{ fontSize: 14 }}>{diffInMinutes}</b>
                分钟
              </div>
            </span>
          );
        default:
          return '提前' + diffInMinutes + '分钟';
      }
    } else {
      // 时间差大于等于 1 小时，继续判断是否超过 24 小时
      const diffInHours = Math.round(diffInMs / (60 * 60 * 1000));
      if (diffInHours >= 24) {
        // 时间差超过 24 小时，转为天
        const diffInDays = Math.round(diffInHours / 24);
        switch (format) {
          case 'format1':
            return (
              <span
                style={{
                  color: '#999',
                  fontSize: 12,
                  display: 'flex',
                  alignItems: 'center',
                  gap: 2,
                }}
              >
                <div style={{ paddingTop: 0 }}>
                  提前
                  <b style={{ fontSize: 14 }}> {diffInDays} </b>天
                </div>
              </span>
            );
          default:
            return '提前' + diffInDays + '天';
        }
      } else {
        // 时间差在 1 小时到 24 小时之间，直接输出小时数

        switch (format) {
          case 'format1':
            return (
              <span
                style={{
                  color: '#999',
                  fontSize: 12,
                  display: 'flex',
                  alignItems: 'center',
                  gap: 2,
                }}
              >
                <div style={{ paddingTop: 0 }}>
                  提前
                  <b style={{ fontSize: 14 }}> {diffInHours} </b>
                  小时
                </div>
              </span>
            );
          default:
            return '提前' + diffInHours + '小时';
        }
      }
    }
  }
}

export function ifdiffs(start, end, currentUser, item) {
  const time = currentUser.waringTime,
    filter = currentUser?.waringfilter;
  const isfinished = item?.status === 0 || item?.status === 1;

  if (!filter) {
    return true;
  }

  if (isfinished) {
    const diffInMs = start.diff(end);
    const diffInHours = Math.round(diffInMs / (60 * 60 * 1000));
    const diffInDays = Math.round(diffInHours / 24);

    return diffInDays < time || diffInDays === time || diffInMs < 0;
  } else {
    return false;
  }
}

export function ifdiffes(start, end, currentUser, item) {
  const time = currentUser.waringTime;
  const isfinished = item?.status === 0 || item?.status === 1;

  if (isfinished) {
    const diffInMs = start.diff(end);
    const diffInHours = Math.round(diffInMs / (60 * 60 * 1000));
    const diffInDays = Math.round(diffInHours / 24);
    if (diffInMs < 0) {
      return 'overtime';
    } else {
      return diffInDays < time || diffInDays === time;
    }
  } else {
    return false;
  }
}

export function formartime(now) {
  // 获取当前时间

  // 根据当前时间判断上午、中午、下午、晚上、凌晨
  const hour = now.hour();
  let timeOfDay = '';

  if (hour >= 5 && hour < 12) {
    timeOfDay = '上午';
  } else if (hour === 12) {
    timeOfDay = '中午';
  } else if (hour > 12 && hour < 18) {
    timeOfDay = '下午';
  } else if (hour >= 18 && hour < 24) {
    timeOfDay = '晚上';
  } else {
    timeOfDay = '凌晨';
  }

  return timeOfDay;
}
